package  
{
	import fl.controls.TextArea;
	import flash.desktop.NativeApplication;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TouchEvent;
	import flash.net.Socket;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import zdc.ui.MovingInertia;
	
	/**
	 * 
	 * @author Metal Slime
	 */
	public class RemoteController extends MovieClip 
	{
		private var _sock:Socket;
		private var _ta:TextArea;
		private var _port:int = 8000;
		private var _host:String = "192.168.1.100";
		private var _iu:MovingInertia;
		private var _id:int = 0;
		
		public function RemoteController() 
		{
			stage? init() : addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			initVars();
			initState();
			initEvents();
			run();
		}
		
		private function initVars():void 
		{
			_sock = new Socket();
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			_iu = new MovingInertia(stage, null, TouchEvent.TOUCH_BEGIN, TouchEvent.TOUCH_END, TouchEvent.TOUCH_MOVE, TouchEvent.TOUCH_OUT);
			//_iu = new MovingInertia(stage, null, MouseEvent.MOUSE_DOWN, MouseEvent.MOUSE_UP, MouseEvent.MOUSE_MOVE, MouseEvent.MOUSE_OUT);
			//_iu.addMoveHook(onMove);
			_iu.addUpHook(onUp);
			_iu.addDownHook(onDown);
			_iu.addWalkHook(onMove);
			_iu.WALK_TYPE = true;
			_iu.minWalkDistX = 20;
			_ta = this.t;
			
		}
		
		private function onUp(...rest):void {
			addText("弹起");
		}
		
		private function onDown(...rest):void {
			addText("按下");
		}
		
		private function onMove(e:Event, dx:Number, dy:Number):void {
			_id++;
			addText("Walk! id:", _id, " x:", dx, " y:", dy);
			_sock.writeUTF(dx + "," + dy + "," + _id);
			_sock.flush();
		}
		
		private function onConnect(e:Event):void 
		{
			addText("已连接成功...");
		}
		
		private function onClose(e:Event):void 
		{
			addText("服务器已关闭...");
		}
		
		private function onSockData(e:ProgressEvent):void 
		{
			try {
				var msg:String = _sock.readUTF();
				addText("收到信息：", msg);
			}
			catch (e:Error) {
				addText("错误：", e);
			}
			
		}
		
		private function onErr(e:IOErrorEvent):void 
		{
			addText("发生错误:", e);
		}
		
		private function initState():void 
		{
			
		}
		
		/**
		 * 添加文本
		 * @param	...rest
		 */
		private function addText(...rest):void {
			_ta.appendText(rest.join(" ") + "\n");
			_ta.verticalScrollPosition = _ta.textHeight;
		}
		
		private function initEvents():void 
		{
			_sock.addEventListener(IOErrorEvent.IO_ERROR, onErr);
			_sock.addEventListener(ProgressEvent.SOCKET_DATA, onSockData);
			_sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErr);
			_sock.addEventListener(Event.CLOSE, onClose);
			_sock.addEventListener(Event.CONNECT, onConnect);
			closeBtn.addEventListener(MouseEvent.CLICK, closeApp);
		}
		
		private function closeApp(e:MouseEvent):void 
		{
			NativeApplication.nativeApplication.exit();
		}
		
		private function run():void 
		{
			_sock.connect(_host, _port);
		}
	}
}